<template>
	<view class="code-page">
		<view class="code-img">
			<image :src="imgurl+'/WeChat/pic7.png'" mode="widthFix"></image>
		</view>
		<view class="input-group">
			<input type="text" v-model="code" placeholder="请输入兑换码">
		</view>
		<button class="sub" @click="toggle('center')">提交</button>
		<!-- <button class="button" type="primary"><text class="button-text">居中</text></button> -->
		<!-- <view class="h1">请输入兑换码</view> -->
		<uni-popup ref="popup" background-color="none">
			<view class="popup-content" @click="gotohome()">
				<view class="grade">
					<view class="grade-number">
						{{score}}
					</view>
					<view class="grade-title">
						积分
					</view>
					<image :src="imgurl+'/WeChat/pic4.png'" mode=""></image>

				</view>
				<view class="h1">
					兑换成功
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgurl: this.serverURL,
				code: '',
				score: 0,
				isauthorized: (uni.getStorageSync('token') ? true : false),
				userinfo: uni.getStorageSync('token'),
			};
		},
		onReady() {},
		methods: {
			toggle(type) {
				let that = this;
				if (that.code === '') {
					uni.showToast({
						title: '请输入兑换码',
						icon: 'error'
					})
				} else if (!that.isauthorized) {
					uni.showModal({
						title: '未登录',
						content: '是否去登录，加入活动',
						success(res) {
							if (res.confirm) {
								uni.reLaunch({
									url: `/pages/home/home?index=4`
								})
							}
						}
					})
				} else {
					uni.request({
						url: that.serverURL + "/index/Draw/subUserCode",
						method: "POST",
						data: {
							id: that.userinfo['id'],
							code: that.code
						},
						success(res) {
							if (res.data.code == 1) {
								that.code = '';
								that.score = res.data.score
								that.$refs.popup.open(type)
								setTimeout(() => {
									uni.reLaunch({
										url: `/pages/home/home?index=1`
									})
								}, 2000)
							} else {
								uni.showToast({
									title: res.data.msg,
									icon: 'error'
								})
							}
						},
						fail(fail) {
							uni.hideLoading();
						}
					});
				}
			},
		}
	}
</script>

<style lang="less">
	.code-page {
		padding: 0px 75rpx;

	}

	.code-img {
		width: 400rpx;
		margin: 150rpx auto 70rpx auto;
	}

	.input-group {
		height: 108rpx;
		width: 100%;
		border-radius: 108rpx;
		background: #f1f1f1;
		border: 2px solid #fff;
		box-shadow: 0rpx 4rpx 10rpx 5rpx rgba(0, 0, 0, 0.3);
		padding: 0px 30rpx;
		box-sizing: border-box;
		margin-top: 20rpx;

		input {
			padding: 0px;
			width: 100%;
			height: 100%;
			line-height: 108rpx;
		}
	}

	.sub {
		background: #00412d;
		color: #fff;
		margin-top: 30rpx;
		width: 300rpx;
		border-radius: 100rpx;
		// 00412d
	}


	.popup-content {
		flex-direction: column;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 486rpx;
		height: 618rpx;
		border-radius: 30rpx;
		overflow: hidden;
		background: #fff;

		.grade {
			width: 315rpx;
			height: 315rpx;
			border-radius: 50%;
			background-image: linear-gradient(to right, #047449, #00412d);
			position: relative;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;

			image {
				width: 107rpx;
				height: 108rpx;
				position: absolute;
				bottom: 0px;
				right: 10rpx;
			}

			.grade-number {
				font-size: 74rpx;
				font-weight: bold;
			}
		}

		.h1 {
			margin-top: 62rpx;
			font-size: 45rpx;
			font-weight: bold;
		}

		.ds {
			font-size: 28rpx;
			margin-top: 10rpx;
		}
	}
</style>
